// pages/car/add/add.js
const form = require("../../../components/utils/formValidation.js");
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var xrSdk = require('../../../vendor/hope-sdk/index.js');
var config = require('../../../config.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showModalStatus: false,
    animationData: "",
    regionArr: [
      "京", "津", "沪", "渝", "蒙", "新",
      "藏", "宁", "桂", "港", "澳", "黑",
      "吉", "辽", "晋", "冀", "青", "鲁",
      "豫", "苏", "皖", "浙", "闽", "赣",
      "湘", "鄂", "粤", "琼", "甘", "陕",
      "贵", "云", "川"
    ],
    regionTxt: "鲁",
    carnumber:'',
    tabIndex : -1,
    brand_id:-1,
    brand_name:'请选择',
    cartype:[],
    typeIndex: 0,
    typeName:'请选择',

    //来源  0 第一次注册添加 1 列表添加  2其他地方添加
    source : 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      //console.log(form);
      this.setData({
        source: options.source || 1
      });
  },
  showModal: function () {
    // 显示遮罩层
    // 创建动画实例 
    var animation = wx.createAnimation({
      duration: 220,
      timingFunction: "linear",
      delay: 0
    })
    //执行第一组动画：Y轴偏移500px后(盒子高度是500px) ，停
    animation.translateY(500).step()
    //导出动画对象赋给数据对象储存
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    this.setData({
      showModalStatus: false
    })
  },
  getRegion: function (e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      regionTxt: this.data.regionArr[index],
      tabIndex: index,
      showModalStatus: false
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  selectBrand:function(){
    wx.navigateTo({
      url: "../../common/brand/brand"
    })
  },
  initCarType:function(brand_id){
    var that = this;
    xrRequest.request(app,{
      url: config.service.getCarType,
      login: true,
      data:{
        brand_id: brand_id
      },
      success: function (res) {
        if (res.data.code == 1) {
          that.setData({
            cartype: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) { },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  bindCarType: function (e) {
    var that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    that.setData({
      typeIndex: e.detail.value,
      typeName: that.data.cartype[e.detail.value].name
    });
  },
  submitForm:function(e){
    var that = this;
    if (that.data.regionTxt == '' || that.data.carnumber==''){
      wx.showToast({
        title: '车牌不能为空',
        icon: 'none',
      })
      return;
    }
    if (that.data.brand_id == -1) {
      wx.showToast({
        title: '车辆品牌不能为空',
        icon: 'none',
      })
      return;
    }
    wx.showLoading({
      title: '请稍候...',
      mask: true
    })
    xrRequest.request(app,{
      url: config.service.addCar,
      login: true,
      hideLoading:true,
      data: {
        plate_area: that.data.regionTxt,
        plate_number: that.data.carnumber,
        brand: that.data.brand_name == '请选择' ? "" : that.data.brand_name,
        model: that.data.typeName == '请选择' ? "" : that.data.typeName
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
          xrSdk.cookie.set('car_id', res.data.data);
          that.goToBack();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res,
          icon: 'none',
        })
        wx.hideLoading();
      },
      authfail: function (e) {
        wx.hideLoading();
        wx.redirectTo({
          url: '../../auth/auth',
        })
      }
    })
  },
  carnumberInput:function(e){
    this.setData({
      carnumber: e.detail.value
    });
  },
  goToBack:function(){
    switch(this.data.source){
      case "0":
        wx.redirectTo({
          url: '../../activity/apply/apply'
        });
        break;
      case "1":
        wx.navigateBack();
        break;
      case "2":

        break;
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})